// pages/user/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    isLogin: false,
    orderCount: {
      unpaid: 0,
      undelivered: 0,
      delivered: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkLoginStatus();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkLoginStatus();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token');
    const userInfo = wx.getStorageSync('userInfo');
    
    if (token && userInfo) {
      this.setData({
        isLogin: true,
        userInfo: userInfo
      });
    } else {
      this.setData({
        isLogin: false,
        userInfo: null
      });
    }
  },

  // 获取手机号
  getPhoneNumber: function (e) {
    wx.navigateTo({
      url: '/pages/user/login/login'
    });
  },

  // 跳转到订单列表
  goToOrderList(e) {
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/order-list/index?type=${type}`
    });
  },

  // 跳转到地址管理
  goToAddress() {
    wx.navigateTo({
      url: '/pages/address/index'
    });
  },

  //跳转在线客服
  goToOnline() {
    wx.navigateTo({
      url: '/pages/online/index'
    });
  },

  // 跳转到关于我们
  goToAbout() {
    wx.navigateTo({
      url: '/pages/about/index'
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            isLogin: false,
            userInfo: null
          });
        }
      }
    });
  },

  // 功能按钮点击事件
  goToOrderInfo() {
    wx.navigateTo({
      url: '/pages/order/list/index'
    });
  },

  goToMemberInfo() {
    wx.navigateTo({
      url: '/pages/member/index'
    });
  },

  goToBenefit() {
    wx.navigateTo({
      url: '/pages/benefits/index'
    });
  },

  goToAddress() {
    console.log("goToAddress");
    wx.navigateTo({
      url: '/pages/address/index'
    });
  },

  goToCooperation() {
    wx.navigateTo({
      url: '/pages/cooperation/index'
    });
  },

  goToCustomerService() {
    // 打开客服会话
    wx.openCustomerServiceChat({
      extInfo: { url: 'https://work.weixin.qq.com/kfid/kfc123456789' },
      corpId: 'ww123456789',
      success(res) {}
    });
  },

  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567'
    });
  }
})